#include "WidSybilView.h"
#include "DataModel.h"
#include "FactoryView.h"
#include "scImpl.h"

FactoryView * FactoryView::inst(0);

#define FCT "FactoryView::get"
FactoryView * FactoryView::get()
{
	if(inst)
		return inst;
	inst=new FactoryView();
	return inst;
}

#undef FCT
#define FCT "FactoryView::FactoryView"
FactoryView::FactoryView()
{
}

#undef FCT
#define FCT "FactoryView::~FactoryView"
FactoryView::~FactoryView()
{
	for(Products::iterator it(products.begin()); it!=products.end(); ++it)
		delete it->second;
}

#undef FCT
#define FCT "FactoryView::addProduct"
bool FactoryView::addProduct(int type, SybilViewCreator creator, SybilViewTranslationCreator transCreator)
{
	TransSybilView * trans((*transCreator)());
	bool res(products.insert(Products::value_type(type, new
		ViewData(type, creator, trans->getTechnicalName(), trans->getDisplayName(), trans->getDescription()))).second);
	delete trans;
	return res;
}

#undef FCT
#define FCT "FactoryView::create"
WidSybilView * FactoryView::create(int type, QWidget * parent) const
{
	Products::const_iterator it(products.find(type));
	if(it==products.end())
		throw new AppException(FCT ": invalid type", true);
	WidSybilView * res((*(it->second->cre))(parent));
	connect(res, SIGNAL(disabledViewClicked(WidSybilView*)), parent, SLOT(checkViewClosureSl(WidSybilView*)));
	return res;
}

#undef FCT
#define FCT "FactoryView::getViewTypes"
std::vector<int> FactoryView::getViewTypes() const
{
	vector<int> res;
	for(Products::const_iterator it(products.begin()); it!=products.end(); ++it)
		res.push_back(it->first);
	return res;
}

#undef FCT
#define FCT "FactoryView::getTechnicalName"
const std::string & FactoryView::getTechnicalName(int type) const
{
	Products::const_iterator it(products.find(type));
	if(it==products.end())
		throw new AppException(FCT ": invalid type", true);
	return it->second->nameTech;
}

#undef FCT
#define FCT "FactoryView::getDisplayName"
QString FactoryView::getDisplayName(int type) const
{
	Products::const_iterator it(products.find(type));
	if(it==products.end())
		throw new AppException(FCT ": invalid type", true);
	return it->second->nameDisp;
}

#undef FCT
#define FCT "FactoryView::getDescription"
QString FactoryView::getDescription(int type) const
{
	Products::const_iterator it(products.find(type));
	if(it==products.end())
		throw new AppException(FCT ": invalid type", true);
	return it->second->desc;
}

